package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.entity.AgentInvitationCategory;
import com.serkol.ams.service.AgentInvitationCategoryService;
import com.serkol.ams.mapper.AgentInvitationCategoryMapper;
import com.serkol.handler.MyLambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【agent_invitation_category】的数据库操作Service实现
* @createDate 2025-02-03 17:10:17
*/
@Service
public class AgentInvitationCategoryServiceImpl extends ServiceImpl<AgentInvitationCategoryMapper, AgentInvitationCategory>
    implements AgentInvitationCategoryService{

    @Override
    public List<Map<String, Object>> findByList() {
        MyLambdaQueryWrapper<AgentInvitationCategory> queryWrapper=new MyLambdaQueryWrapper<>();
        queryWrapper.select(AgentInvitationCategory::getId).as("value").select(AgentInvitationCategory::getState,AgentInvitationCategory::getStatus).select(AgentInvitationCategory::getName).as("label").select(AgentInvitationCategory::getId).as("id");
        List<Map<String, Object>> maps = this.listMaps(queryWrapper);
        List<Map<String, Object>> collect = maps.stream().peek(map -> map.putAll(Map.of("key", map.get("id"),"danger", (int) map.getOrDefault("state", 0) == 1))
).collect(Collectors.toList());
        return collect;
    }

    @Override
    public boolean agentCategoryState(long id) {
        AgentInvitationCategory agentInvitationCategory = this.getById(id);
        if (agentInvitationCategory != null) {
            LambdaUpdateWrapper<AgentInvitationCategory> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(AgentInvitationCategory::getState, 1).set(AgentInvitationCategory::getState, 0);
            boolean b = this.update(updateWrapper);
            if(b) {
                agentInvitationCategory.setState(1);
                this.updateById(agentInvitationCategory);
            }
            return b;
        }
        return false;
    }
}




